require "parser/metaparser"

require "timer"
require "hideinv"
require "quotes"
require "para"
require "dash"
require "sprites"
require "prefs"

dofile "lib/core.lua"
dofile "lib/inventory.lua"
dofile "lib/scanner.lua"
dofile "lib/string.lua"
dofile "lib/util.lua"
dofile "lib/verbs.lua"
dofile "lib/words.lua"
dofile "lib/rooms.lua"
dofile "lib/fonts.lua"
dofile "lib/drawing.lua"
dofile "lib/musplayer.lua"
dofile "lib/templates.lua"

morph.debug = true
morph.yo = false
morph:init(
  "main.lua", 
  "game/common.lua",
  "game/prologue.lua",
  "game/episode01.lua",
  "game/episode02.lua",
  "game/episode03.lua",
  "game/episode04.lua",
  "game/episode05.lua",
  "game/episode06.lua",
  "game/episode07.lua",
  "game/episode08.lua",
  "game/episode09.lua",
  "game/episode10.lua",
  "game/episode11.lua",
  "game/episode12.lua",
  "game/episode13.lua",
  "game/episode14.lua",
  "game/episode15.lua",
  "game/episode16.lua",
  "game/episode17.lua",
  "game/episode18.lua",
  "game/episode19.lua",
  "game/episode20.lua",
  "game/episode21.lua",
  "game/episode22.lua",
  "game/episode23.lua",
  "game/episode24.lua",
  "game/episode25.lua",
  "game/episode26.lua")

title = font("theme/sans-b.ttf", 18);
quest = font("theme/sans-b.ttf", 20);
small = font("theme/sans.ttf", 12);
small_fn = sprite.font("theme/sans.ttf", 14);
regular = sprite.font("theme/sans.ttf", 14);

fbold = font("theme/sans-b.ttf", 14);
fitalic = font("theme/sans-i.ttf", 14);

gui = {}
gui.fading = 10;
parser.scroll = true
parser.debug = true
parser.cmd_history = true
parser.empty_warning = quest:txt("?", "gray")
parser.hintinv = true
parser.inv_delim = " " .. small:txt("|", "#666666") .. " "
space_size = sprite.text_size(regular, " ");
parser.inv_delim_size = sprite.text_size(small_fn, "|") + space_size*2;
parser.inv_rows = 2;
-- parser.filter = true

format.para_space = "";
--[[parser.hintfmt = function(s,w)
  local ret = "";
  for t in string.gmatch(w, "[^%s]+") do
    if ret ~= "" then
      ret = ret .. " " .. regular:txt(t,"cyan");
    else
      ret = regular:txt(t,"cyan");
    end
  end
  return ret;
end
parser.selffmt = function(s,w)
  return w;--regular:txt(w,"white");
end]]

game.fading = function(s)
  if here().fading then
    return here().fading;
  elseif player_moved() then
    if _nofading then
      _nofading = nil;
      return false;
    else
      return gui.fading;
    end
  else
    return false;
  end
end

instead.get_title = function()
  return nil;
end

function start()
  --if _light then
  --  input.cursor = img "theme/cursor2.gif"
  --else
  input.cursor = img "theme/cursor.gif"
  --end
  hero.clock = os.time();
end

--[[function stead.savename()
  local diff = os.time() - hero.clock;
  local ot = nvl(hero._time,0);
  hero._time = ot + diff;
  local mins = math.ceil(hero._time / 60);
  local hrs = math.floor(mins / 60);
  if hrs > 0 then
    tstr = hrs .. " ч. " .. mins - hrs*60;
  else
    tstr = mins;
  end
  local str = " (Время игры: " .. tstr .. " мин.)";
  hero.clock = os.time();
  hero._timestr = str;
  print(tostring(tstr):replace("ч.","hrs").." mins");
  return str;
end]]

function beatgame()
  local diff = os.time() - hero.clock;
  local ot = nvl(hero._time,0);
  hero._time = ot + diff;
  local mins = math.ceil(hero._time / 60);
  local hrs = math.floor(mins / 60);
  if hrs > 0 then
    tstr = hrs .. " ч. " .. mins - hrs*60 .. " мин.";
  else
    tstr = mins .. " мин.";
  end
  prefs["lasttime"] = tstr;
  local plays = prefs["games"];
  if plays == nil then
    plays = 0;
  end
  plays = plays + 1;
  prefs["games"] = plays;
  prefs["stat_exams"] = nvl(hero._exams,0);
  prefs["stat_wrecks"] = nvl(hero._wrecks,0);
  prefs["stat_attacks"] = nvl(hero._attacks,0);
  prefs["stat_rets"] = nvl(hero._rets,0);
  prefs["stat_cracks"] = nvl(hero._cracks,0);
  prefs["stat_activs"] = nvl(hero._activs,0);
  prefs["stat_move"] = nvl(hero._move,0);
  prefs["stat_talks"] = nvl(hero._talks,0);
  prefs["stat_think"] = nvl(hero._think,0);
  prefs["stat_sits"] = nvl(hero._sits,0);
  prefs["stat_lies"] = nvl(hero._lies,0);
  prefs["stat_cry"] = nvl(hero._cry,0);
  prefs["stat_around"] = nvl(hero._around,0);
  prefs["stat_wait"] = nvl(hero._wait,0);
  prefs["stat_look"] = nvl(hero._look,0);
  prefs["stat_stands"] = nvl(hero._stands,0);
  prefs["stat_press"] = nvl(hero._press,0);
  prefs["stat_walks"] = nvl(hero._walks,0);
  prefs["stat_takes"] = nvl(hero._takes,0);
  prefs["stat_throw"] = nvl(hero._throw,0);
  prefs:store();
end

function did_beatgame()
  prefs:load();
  return prefs["lasttime"] ~= nil;
end

function get_lasttime()
  prefs:load();
  return prefs["lasttime"];
end

function unlock_(ach)
  return function()
    prefs["a_"..ach] = true;
    prefs:store();
  end
end

function unlocked(ach)
  prefs:load();
  return prefs["a_"..ach]==true;
end

function getstat(st)
  prefs:load();
  return nvl(prefs["stat_"..st],0);
end
